package com.sakura;

import java.util.Random;

/**
 * Created by apple on 2018/12/11.
 */
public class Test {
    //测试回文数
    public static boolean isPalindrome(int x) {
        if(x < 0){
            return false;
        }
        int result = 0;
        while(x != 0){
            int a = x%10;
            x = x/10;

            result = result*10 + a;
        }
        System.out.println(result);
        if(result == x){
            return true;
        }else{
            return false;
        }
    }

    //14. 最长公共前缀
    public static String longestCommonPrefix(String[] strs) {
        StringBuffer prefix = new StringBuffer();
        if(strs.length == 0){
            return prefix.toString();
        }
        char[] firstWord = strs[0].toCharArray();
        for(int i = 0;i<firstWord.length;i++){
            int k = 0;
            for(int j=1;j<strs.length;j++){
                char[] word = strs[j].toCharArray();
                if(i<word.length && firstWord[i] == word[i]){
                    k++;
                }else{
                    break;
                }
            }
            if(k == (strs.length - 1)){
                prefix = prefix.append(firstWord[i]);
            }else{
                break;
            }
        }

        return prefix.toString();
    }

    public static  int climbStairs(int n) {
        if(n == 1){
            return 1;
        }
        if(n == 2){
            return 2;
        }

        return climbStairs(n-2) + climbStairs(n-1);

    }

    //字母转ASCII值
    public static void testASCII(){
        char a = 'a';
        byte x = (byte)a;
        int b = x-0;
        System.out.println(x);


    }

    public static void testString(){
        String a = "123";
        char[] array = a.toCharArray();
        char first = a.charAt(0);
        System.out.println(String.valueOf(first));

        Random random = new Random();
        System.out.println(random.nextInt(10));
    }

    public static void main(String[] args) {
//        isPalindrome(121);
//        String a  = "123";
//        a.toCharArray();

//        String[] strs = new String[]{"aca","cba"};
//        System.out.println(longestCommonPrefix(strs));;

//        StringBuffer a = new StringBuffer();
//        a.toString();

//        System.out.println(climbStairs(44));

        testASCII();

        testString();
    }
}
